<?php

namespace Admin\Controller;
use Think\Controller;

class CaseController extends Controller{
    

    /*
	案例列表展示
    */
    public function lists(){

        $page = I('post.page');

        $page_size = I('post.page_size');

        $count = M('case')->count();

        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
    	$result = M('case')->limit($limitStart,$page_size)->Getfield('id,product_name,title,logo');

    	if($result !== false ){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

    	$this->ajaxReturn($ajaxReturn);

    }

    /*
	添加
    */

    public function add(){

    	$product_name = I('post.product_name');
    	$logo = I('post.logo');
    	$case_img = I('post.case_img');
    	$details_img = I('post.details_img');
    	$title = I('post.title');
    	$introduce = I('post.introduce');
    	$litigation = I('post.litigation');
    	$location = I('post.location');
    	$spread_strategy = I('post.spread_strategy');
    	$suitable_product = I('post.suitable_product');
    	$spread = I('post.spread');
    	$opinion = I('post.opinion');
    	$sales_effect = I('post.sales_effect');

    	$data['product_name'] = $product_name;
    	$data['logo'] = $logo;
    	$data['case_img'] = $case_img;
    	$data['details_img'] = $details_img;
    	$data['title'] = $title;
    	$data['introduce'] = $introduce;
    	$data['litigation'] = $litigation;
    	$data['location'] = $location;
    	$data['spread_strategy'] = $spread_strategy;
    	$data['suitable_product'] = $suitable_product;
    	$data['spread'] = $spread;
    	$data['opinion'] = $opinion;
    	$data['sales_effect'] = $sales_effect;

    	$result = M('case')->add($data);

    	if($result!=false){

    		$ajaxReturn['code'] = 1;
    		$ajaxReturn['msg'] = "SUCCESS";
    		$ajaxReturn['data'] = $result;

    	}else{

    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = "新增失败！";

    	}

    	$this->ajaxReturn($ajaxReturn);

    }

    /*
	案例删除
    */

    public function delete(){

    	$id = I('post.id');

    	$case = M('case');

    	$result = $case->where('id='.$id)->delete($id);

    	if($result!=false){

    		$ajaxReturn['code'] = 1;
    		$ajaxReturn['msg'] = "SUCCESS";

    	}else{

    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = "删除失败！";

    	}

    	$this->ajaxReturn($ajaxReturn);

    }

    /*
    案例修改展示列表
    */

    public function infos(){

        $id = I('post.id');

        $case = M('case');

        $result = $case->where('id='.$id)->find();

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "展示信息失败";

        }

        $this->ajaxReturn($ajaxReturn   );

    }

    /*
    案例修改
    */
    
    public function save(){

        $id = I('post.id');
        $product_name = I('post.product_name');
        $logo = I('post.logo');
        $case_img = I('post.case_img');
        $details_img = I('post.details_img');
        $title = I('post.title');
        $introduce = I('post.introduce');
        $litigation = I('post.litigation');
        $location = I('post.location');
        $spread_strategy = I('post.spread_strategy');
        $suitable_product = I('post.suitable_product');
        $spread = I('post.spread');
        $opinion = I('post.opinion');
        $sales_effect = I('post.sales_effect');

        $data['product_name'] = $product_name;
        $data['logo'] = $logo;
        $data['case_img'] = $case_img;
        $data['details_img'] = $details_img;
        $data['title'] = $title;
        $data['introduce'] = $introduce;
        $data['litigation'] = $litigation;
        $data['location'] = $location;
        $data['spread_strategy'] = $spread_strategy;
        $data['suitable_product'] = $suitable_product;
        $data['spread'] = $spread;
        $data['opinion'] = $opinion;
        $data['sales_effect'] = $sales_effect;

        $case = M('case');

        $result = $case->where('id='.$id)->save($data);

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "操作失败！";

        }

        $this->ajaxReturn($ajaxReturn);

    }

}